import { BrowserWindow, shell, screen } from 'electron'
import { is } from '@electron-toolkit/utils'
import { join, } from 'path'
import BaseWindow from './components/BaseWindow'

export default class DiscussionGroupWindow extends BaseWindow {
  //创建窗口
  create(): void {
    if (this.frame !== null && !this.frame.isDestroyed()) return
    this.frame = new BrowserWindow({
      width: 220,
      height: 100,
      show: false,
      alwaysOnTop: true, // 窗口置顶 默认值为false
      autoHideMenuBar: true, // 隐藏菜单栏 默认值为false
      hasShadow: false, // 禁止窗口阴影 默认值为true
      skipTaskbar: true, // 隐藏任务栏 默认值为false
      useContentSize: true, // 使用内容大小 默认值为false
      movable: false, // 禁止窗口移动 默认值为true
      resizable: false, // 禁止窗口缩放 默认值为true
      transparent: true, // 窗口透明 默认值为false
      focusable: true,
      webPreferences: {
        preload: join(__dirname, '../preload/index.js'),
        sandbox: false, // 禁用沙箱模式
        zoomFactor: 1.0, // 禁止缩放
      }
    })

    this.frame!.on('blur', () => {
      this.frame!.hide()
      const aiIconWindow = this.getWindow('aiIconWindow')
      aiIconWindow.frame.webContents.send('destroy-discussion-group')
    })

    this.frame!.on('show', () => {
      setTimeout(() => {
        this.frame!.focus()
      }, 50)
    })

    const routerPath = '/discussionGroup'
    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
      this.frame.loadURL(process.env['ELECTRON_RENDERER_URL'] + '/#' + routerPath)
    } else {
      this.frame.loadFile(join(__dirname, '../renderer/index.html'), {
        hash: routerPath
      })
    }

    // 设置窗口打开链接的处理方式
    this.frame.webContents.setWindowOpenHandler((details) => {
      shell.openExternal(details.url)
      return { action: 'deny' }
    })
  }
  onDisplay (x, y) {
    const allDisplays = screen.getAllDisplays()
    let screenBounds = allDisplays.reduce((acc, display) => {
      const bounds = display.bounds
      acc.minX = Math.min(acc.minX, bounds.x)
      acc.maxX = Math.max(acc.maxX, bounds.x + bounds.width)
      acc.minY = Math.min(acc.minY, bounds.y)
      acc.maxY = Math.max(acc.maxY, bounds.y + bounds.height)
      return acc
    }, {
      minX: Infinity,
      maxX: -Infinity,
      minY: Infinity,
      maxY: -Infinity,
    })

    const [winW] = this.frame!.getSize() // 获取窗口的宽度和高度
    // 获取当前鼠标所在屏幕
    const currentDisplay = screen.getDisplayNearestPoint({ x, y })
    const workArea = currentDisplay.workArea

    const mouseRange = { minX: 0, maxX: 0, minY: 0, maxY: 0 }
    mouseRange.minX = screenBounds.minX
    mouseRange.maxX = screenBounds.maxX
    mouseRange.minY = workArea.y
    mouseRange.maxY = workArea.height + workArea.y

    x = x - 60
    if (x < mouseRange.minX) x = mouseRange.minX
    if (x + winW > mouseRange.maxX) x = mouseRange.maxX - winW

    y = y - 110
    if (y < mouseRange.minY) y = mouseRange.minY + 110
    this.frame?.setPosition(x, y)

    this.frame?.show()
    setTimeout(() => {
      if (this.frame) this.frame.hide()
    }, 5000)
  }
}
